package com.nf.health.app.customview;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.adapter.OnMyClickListener2;
import com.nf.health.app.intervention_details.bean.InterventionBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by hepan on 2016/11/16.
 * 干预详情的popupWindow
 */
public class InterventionPopupWindow extends PopupWindow implements View.OnClickListener {

    private MyListView lvMenu;
    private ListView lvTop;
    private MyListView lvBottom;
    private TextView tvHistory;
    private TextView tvBuyMore;

    private View rootView;
    private Activity ctx;
    private int mFragmentIndex;
    //private int mInterventionIndex;
    private List<InterventionBean> interventions;//干预方案
    private String[] menus = new String[]{"封面", "目录", "健康信息汇总", "健康行动改善指南", "膳食方案", "运动方案",
            "经络方案", "音乐方案", "监测计划",  "医嘱", "干预问卷调查"};
    private OnMyClickListener2 listener;
    private MyListAdapter topListAdapter;
    private MyListAdapter2 bottomListAdapter;
    private List<InterventionBean> topList;
    private List<InterventionBean> bottomList;
    private MenuAdapter menuAdapter;
    private LinkedList<String> menuLinkList =  new LinkedList<>();
    private List<String> menuList = new ArrayList<>();
   // private LinearLayout ll_c_contentView;
    /**
     * @param act
     * @param interventions 干预方案名称
     */
    public InterventionPopupWindow(Activity act, List<InterventionBean> interventions, int mFragmentIndex) {
        super(act);
        ctx = act;
        this.interventions = interventions;
        this.mFragmentIndex = mFragmentIndex;
        rootView = LayoutInflater.from(act).inflate(R.layout.pop_more_intervention, null);

        this.setContentView(rootView);
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setAnimationStyle(android.R.style.Animation_Dialog);
        this.setBackgroundDrawable(new BitmapDrawable());//必要,保证背景是自定义的.9patch图
        menuList.addAll(Arrays.asList(menus));
        initView();
        initListener();

    }
    private  void refreshList(String type){
        if(!TextUtils.isEmpty(type)){//1 正常健康服务包  3 或 4 是活动
            menuLinkList.clear();
            if(type.equals("1")){
                menuLinkList.addAll(menuList);
            }else{
                menuLinkList.addAll(menuList.subList(0,menuList.size() -1 - 1));
            }
        }
    }
    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha)
    {

        // 产生背景变暗效果
        WindowManager.LayoutParams lp = ctx.getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ctx.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        ctx.getWindow().setAttributes(lp);


      /*  WindowManager.LayoutParams lp = ctx.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ctx.getWindow().setAttributes(lp);*/
    }

    private void initView() {
        topList = new ArrayList<>();
        bottomList = new ArrayList<>();
        lvTop = (ListView) rootView.findViewById(R.id.lv_top);
        lvMenu = (MyListView) rootView.findViewById(R.id.lv_menu);
        lvBottom = (MyListView) rootView.findViewById(R.id.lv_bottom);
        tvHistory = (TextView) rootView.findViewById(R.id.tv_history);
        tvBuyMore = (TextView) rootView.findViewById(R.id.tv_buy_more);
       // ll_c_contentView = (LinearLayout) rootView.findViewById(R.id.ll_c_contentView);
        InterventionBean interventionBean = interventions.get(0);
        String type = interventionBean.getType();
        refreshList(type);
        if(menuAdapter == null){
            menuAdapter = new MenuAdapter(menuLinkList);
            lvMenu.setAdapter(menuAdapter);
        }else{
            menuAdapter.setMyNotifyDataChanged(menuLinkList);
        }
        //给上下两个lv设置适配器
        setTopAndBottomAdapter(0);
        backgroundAlpha(0.5f);
    }

   /* private void setTopAndBottomAdapter(int index) {
        if (index + 1 > interventions.size()) {
            return;
        }
        if (listener != null) {
            listener.onClick(index, "2");//2代表,健康方案
        }
        lvTop.setAdapter(new MyListAdapter(interventions.subList(0, index + 1)));
        lvTop.setSelection(lvTop.getAdapter().getCount());
        if (interventions.size() > index + 1) {
            lvBottom.setAdapter(new MyListAdapter2(interventions.subList(index + 1, interventions.size())));
        }
        dismiss();
    } */
    private void setTopAndBottomAdapter(int index) {
        if (index + 1 > interventions.size()) {
            return;
        }
        InterventionBean selectItem = interventions.get(0);
        /*if (listener != null) {
            listener.onClick(index, "2",);//2代表,健康方案
        }*/
        if(topListAdapter == null){
            topList.add(selectItem);
            topListAdapter = new MyListAdapter(topList);
            lvTop.setAdapter(topListAdapter);
        }else{
            topListAdapter.setMyTopNotifyDataChanged(topList);
        }

        if (interventions.size() > index + 1) {
            if(bottomListAdapter == null){
                bottomList.addAll(interventions.subList(index + 1, interventions.size()));
                bottomListAdapter = new MyListAdapter2(bottomList);
                lvBottom.setAdapter(bottomListAdapter);
            }else{
                bottomListAdapter.setMyBottomNotifyDataChanged(bottomList);
            }
        }

        dismiss();
    }
    private void setBottomAdapter(int index) {
        if (index + 1 > interventions.size()) {
            return;
        }
        InterventionBean selectItem = bottomList.get(index);
        InterventionBean selecttop = topList.get(0);
        topList.set(0,selectItem);
        topListAdapter.setMyTopNotifyDataChanged(topList);
        bottomList.set(index,selecttop);
            for (int i = 0; i < interventions.size(); i++) {
                if(interventions.get(i).equals(selectItem)){
                    index = i;
                    break;
                }
            }
        bottomListAdapter.setMyBottomNotifyDataChanged(bottomList);
        if (listener != null) {
            listener.onClick(index, "2",selectItem);//2代表,健康方案
        }
        String type = selectItem.getType();
        refreshList(type);
        if(menuAdapter == null){
            menuAdapter = new MenuAdapter(menuLinkList);
            lvMenu.setAdapter(menuAdapter);
        }else{
            menuAdapter.setMyNotifyDataChanged(menuLinkList);
        }

        dismiss();
    }
    private void setTopAdapter() {
        dismiss();
    }

    private void initListener() {
        lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listener != null) {
                    listener.onClick(position, "0",null);//0代表menus
                }
                dismiss();
            }
        });
        lvTop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               /* mInterventionIndex = position;*/
                /*setTopAndBottomAdapter(mInterventionIndex);*/
                setTopAdapter();
            }
        });
        lvBottom.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                /*mInterventionIndex += (position + 1);
                if (lvBottom.getAdapter().getCount() == position + 1) {
                    lvBottom.setAdapter(null);
                    setTopAndBottomAdapter(mInterventionIndex);
                } else {
                    setTopAndBottomAdapter(mInterventionIndex);
                }*/
                setBottomAdapter(position);
            }
        });
        tvHistory.setOnClickListener(this);
        tvBuyMore.setOnClickListener(this);
    }

    public void setOnMyItemClickListener(OnMyClickListener2 listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        this.dismiss();
        if (listener != null) {
            listener.onClick(v.getId(), "1",null);//1代表历史方案,购买更多
        }
    }


    /**
     * 执行方案中plans适配器 数据写死的
     */
    private class MenuAdapter extends BaseAdapter {
        private LinkedList<String> linkList;

        public MenuAdapter(LinkedList<String> linkList) {
            this.linkList = linkList;
        }

        @Override
        public int getCount() {
            return linkList.size();
        }

        @Override
        public Object getItem(int position) {
            return linkList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MenuAdapterViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(ctx, R.layout.simple_textview, null);
                holder = new MenuAdapterViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (MenuAdapterViewHolder) convertView.getTag();
            }
            if (position == mFragmentIndex) {
                holder.text1.setTextColor(ctx.getResources().getColor(R.color.light_theme_color));
            } else {
                holder.text1.setTextColor(ctx.getResources().getColor(R.color.alpha_30_black));
            }
            holder.text1.setText(linkList.get(position));
            return convertView;
        }
        public void setMyNotifyDataChanged(LinkedList<String> linkList) {
            this.linkList = linkList;
            notifyDataSetChanged();
        }
    }

    private class MenuAdapterViewHolder {
        TextView text1;

        MenuAdapterViewHolder(View view) {
            text1 = (TextView) view.findViewById(R.id.text1);
        }
    }

    /**
     * top lv适配器,简单的展示方案名称
     */
    private class MyListAdapter extends BaseAdapter {
        private List<InterventionBean> lists;

        public MyListAdapter(List<InterventionBean> menus) {
            this.lists = menus;
        }

        @Override
        public int getCount() {
            if (lists == null) {
                return 0;
            }
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyListAdapterViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(ctx, R.layout.simple_theme_textview, null);
                holder = new MyListAdapterViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (MyListAdapterViewHolder) convertView.getTag();
            }
           InterventionBean interventionBean  = lists.get(position);
            if (position == lists.size() - 1) {
                holder.text1.setText("√ " + interventionBean.getName());
            } else {
                holder.text1.setText(interventionBean.getName());
            }

            return convertView;
        }
        public void setMyTopNotifyDataChanged(List<InterventionBean>  menus) {
            this.lists = menus;
            notifyDataSetChanged();
        }
    }

    /**
     *  bottom lv适配器,简单的展示方案名称
     */
    private class MyListAdapter2 extends BaseAdapter {
        private List<InterventionBean>  lists;

        public MyListAdapter2(List<InterventionBean>  menus) {
            this.lists = menus;
        }

        @Override
        public int getCount() {
            if (lists == null) {
                return 0;
            }
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyListAdapterViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(ctx, R.layout.simple_theme_textview, null);
                holder = new MyListAdapterViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (MyListAdapterViewHolder) convertView.getTag();
            }
            InterventionBean interventionBean  = lists.get(position);
            holder.text1.setText(interventionBean.getName());

            return convertView;
        }
        public void setMyBottomNotifyDataChanged(List<InterventionBean> menus) {
            this.lists = menus;
            notifyDataSetChanged();
        }
    }

    private class MyListAdapterViewHolder {
        TextView text1;

        MyListAdapterViewHolder(View view) {
            text1 = (TextView) view.findViewById(R.id.text1);
        }
    }

}
